package io.plus.utils.thread.lock_capable_task;

import io.plus.utils.queue.Queue;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executor;

@Slf4j
public abstract class AbstractQueueTask<T> extends AbstractTask {

    public AbstractQueueTask(Executor executor) {
        super(executor);
    }

    /**
     * 获取消息队列
     */
    public abstract Queue<T> getQueue();

    public boolean add(T t) {
        if (this.isCanceled()) {
            log.error("任务已经取消");
            return false;
        }

        return getQueue().add(t);
    }

    /**
     * 清空处理的队列消息
     */
    public void clearQueue() {
        if (getQueue() != null) {
            getQueue().clear();
        }
    }

    @Override
    public boolean isNeededExecute() {
        return (getQueue() != null && !getQueue().isEmpty()) && !this.isCanceled();
    }

}
